package net.zjitc.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import net.zjitc.pojo.Category;
import net.zjitc.pojo.Notice;
import net.zjitc.pojo.Product;
import net.zjitc.service.NoticeService;
import net.zjitc.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: KingCc
 * @Date: 2019/6/10 21:18
 */

@Controller
@RequestMapping("/product")
public class ClientProductsController {
    @Autowired
    ProductService productService;
    @Autowired
    NoticeService noticeService;

    @RequestMapping("/fenlei")
    public String fenlei(Model model){
        List<Category> allCategory = productService.findAllCategory();
        model.addAttribute("list",allCategory);
        return "/suyaoyajiu/gallery.jsp";
    }
    @RequestMapping("chazhao")
    public String chazhao(Model model,Category category,Integer currentPage){
        List<Product> byCid = productService.findProductByCid(category.getId());
        model.addAttribute("list",byCid);
        if(currentPage==null){
            currentPage=1;
        }
        //导航分类

        List<Product> allProduct = productService.findAllProduct();

        //当前页的信息
        PageHelper.startPage(currentPage,4);
        PageInfo pageInfo = new PageInfo(allProduct);
        model.addAttribute("pageInfo",pageInfo);
        return "/suyaoyajiu/beckoning2.jsp";
    }
    @RequestMapping("/index")
    public String index(Model model){
        List<Category> categoryList = productService.findAllCategory();
        Notice notice = noticeService.descNoticeDate();
        ArrayList<Object> list = new ArrayList<>();
        model.addAttribute("cList",categoryList);
        model.addAttribute("notice",notice);
        model.addAttribute("pList",list);
        return "/suyaoyajiu/index.jsp";
    }
    @RequestMapping("/findProductById")
    public String findProductById(String id,Model model){
        Product product = productService.findProductById(id);
        model.addAttribute("p",product);
        return "/suyaoyajiu/single.jsp";
    }
    /*查找分页*/
    @RequestMapping("/pageProduct")
        public String pageProducts(Integer currentPage,
                               Model model){
        if(currentPage==null){
            currentPage=1;
        }
        //导航分类
        List<Category> categoryList = productService.findAllCategory();
        model.addAttribute("cList",categoryList);

        //查找当前分类名字
        List<Product> allProduct = productService.findAllProduct();

        //当前页的信息
        PageHelper.startPage(currentPage,6);
        PageInfo pageInfo = new PageInfo(allProduct);
        model.addAttribute("pageInfo",pageInfo);
        return "/suyaoyajiu/beckoning.jsp";
    }
}
